import httplib2
from pprint import pprint

from Ft.Xml.XPath import Conversions
def get_http_document(context, uri, username=None, password=None):
    result = []
    sheet = context.processor.stylesheet
    uri = Conversions.StringValue(uri)
    u = Conversions.StringValue(username)
    p = Conversions.StringValue(password)
    h = httplib2.Http('.cache')
    if u and p:
        h.add_credentials(u, p)
    headers, content = h.request(uri, 'GET')
    if headers.get('status') == '200' and content:
        return content
    return '<none />'
    
    
from urlparse import urljoin

def urljoin(context, first, second):
    f = Conversions.StringValue(first)
    s = Conversions.StringValue(second)
    return urljoin(f, s)

# proc = Processor.Processor()
# proc.registerExtensionFunction(
#     u'http://ionrock.org/ns/http-document',
#     u'get-http-document', get_http_document
# )
